using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Comp476Project
{

    public class MainGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        
        // Debug
        bool FULL_SCREEN = true;
        bool MOUSE_VISIBILITY = true;

        // Game State
        public enum GAME_STATE { TITLE, SELECT, IN_GAME, GAMEOVER };
        public static GAME_STATE STATE;

        // Entities
        Model blueChar, redChar, blueTowerM, redTowerM;
        //Camera camera;
        Matrix projection;
        Hud hud;
        int winWidth, winHeight;
        SpriteFont hudFont, bigFont;
        Texture2D button;
        //List<Character> characters;
        //Sun sun;
        Skybox skyBox;
        Map map;
        Player player;
        Minion mage;
        public bool endGame, winner;

        // Sounds
        Song bg, win;
        SoundEffect death1, death2, shot, defeat;
       
        Team blueTeam, redTeam;
        Primitives3D.SpherePrimitive sphere;

        //HQs
        //Entity blueHQ, redHQ;
        //List<Entity> blueTeam, redTeam;

        public MainGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // Screen Setup
            if (FULL_SCREEN)
            {
                winWidth = 1920;
                winHeight = 1080;
                graphics.IsFullScreen = true;
            }
            else
            {
                winWidth = 800;
                winHeight = 600;
            }
            graphics.PreferredBackBufferWidth = winWidth;
            graphics.PreferredBackBufferHeight = winHeight;
            projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), (float)(winWidth) / (float)(winHeight), 0.001f, 500f);
        }

        protected override void Initialize()
        {


            winner = false;
            endGame = false;

            //camera = new Camera();
            this.IsMouseVisible = MOUSE_VISIBILITY;
            STATE = GAME_STATE.IN_GAME;
            Window.Title = "COMP 476 - Project (Sugi, Mike, Bachir)";
            //characters = new List<Character>();
            
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            // Load Models
            //map = Content.Load<Model>("arena");
            //blueTeam = new List<Entity>();
            //redTeam = new List<Entity>();
            blueChar = Content.Load<Model>("models/purpleChampion");
            //redChar = Content.Load<Model>("models/purpleChampion");
            //blueTowerM = Content.Load<Model>("models/blueTower");
            //redTowerM = Content.Load<Model>("models/redTower");


            sphere = new Primitives3D.SpherePrimitive(GraphicsDevice, 1, 4);
            player = new Player(sphere,projection, blueChar, new Vector3(0.001f, 0f, -0.698f));
            player.size = 0.01f;

            
            // Load textures
            button = Content.Load<Texture2D>("button");

            // Load Font
            hudFont = Content.Load<SpriteFont>("HudFont");
            bigFont = Content.Load<SpriteFont>("BigFont");

            // Initialize Sun
            //sun = new Sun();

            // Initialize Skybox
            skyBox = new Skybox("skybox/Sky", Content);

            // Load Map
            map = new Map(ref projection, Content, GraphicsDevice);

         

            // Initialize Hud
            hud = new Hud(ref button, winWidth, winHeight, hudFont, bigFont, ref player);

            //mage = new Minion(projection, redChar, new Vector3(0.001f, 0f, -0.698f), map);

            //Loading Teams
            blueTeam = new Team(GraphicsDevice,projection, Content, Color.Blue, map);
           
            blueTeam.addPlayer( player);
         
            

            redTeam = new Team(GraphicsDevice,projection, Content, Color.Red, map);

            //blueTeam.squadTimer = 1000000;

            // Sounds
            bg = Content.Load<Song>("sound/axtoncrolley_nodens-field-song");
            win = Content.Load<Song>("sound/kastenfrosch_gewonnen2");
            death1 = Content.Load<SoundEffect>("sound/slime-death");
            death2 = Content.Load<SoundEffect>("sound/michel88_deaths");
            shot = Content.Load<SoundEffect>("sound/anton_kleiner-reverse");
            defeat = Content.Load<SoundEffect>("sound/tubular-bell-of-death");
            MediaPlayer.Play(bg);
            MediaPlayer.IsRepeating = true;
        }

        protected override void UnloadContent()
        {
            
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            player.mapYPosition(map.grid,map.mapSize);
          
            // Update Elements
            //camera.Update(player.position);
            //sun.Update(gameTime);

            // Update Hud
            int n = hud.Update( endGame,winner );
            if (!endGame)
            {
                player.update(gameTime);
                player.handleInput(ref MOUSE_VISIBILITY);
                this.IsMouseVisible = MOUSE_VISIBILITY;
                //mage.update(gameTime);



                blueTeam.update(gameTime, redTeam.assets);
                redTeam.update(gameTime, blueTeam.assets);
                if (blueTeam.HQ.health <= 0) 
                {
                    endGame = true;
                    defeat.Play();
                }
                if (redTeam.HQ.health <= 0)
                {
                    endGame = true;
                    winner = true;
                    MediaPlayer.Stop();
                    MediaPlayer.Play(win);
                    MediaPlayer.IsRepeating = false;
                }
            }
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            // SKYBOX
            GraphicsDevice.RasterizerState = RasterizerState.CullClockwise;
            skyBox.Draw(player.camera.view, projection, new Vector3(0, 0, 0)/*camera.pos*/);
            GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;

            // Draw Map
            map.Draw(player.camera.view);

            // Draw Game Elements
            player.draw(player.camera.view);
            //mage.draw(player.camera.view);
            blueTeam.draw(player.camera.view, player.camera.pos);
            redTeam.draw(player.camera.view, player.camera.pos);
            
         
            // Draw Hud
            hud.Draw(spriteBatch, ref player.camera, gameTime);
            // Fix Rendering back for 3D
            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            //GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
            //GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
            base.Draw(gameTime);
        }//end of draw

      

    }
}
